package behaviorTree

import "fmt"

type Sequence struct {
	*BaseNode
}

func NewSequence() *Sequence {
	rlt := &Sequence{
		BaseNode: NewBaseNode(fmt.Sprintf("Seq%d", GetNextId("Sequence")), NodeType_Sequence),
	}

	rlt.CheckValidationFunc = rlt.checkChildCntOver0

	return rlt
}

func (self *Sequence) Enter(data IRuntimeBTData, stateIn NodeState, localDataIn interface{}) (childNode INode, state NodeState, localDataOut interface{}) {
	switch stateIn {
	case BEGIN:
		return self.children[0], RUNNING, int32(0)

	case SUCCESS:
		curIdx := localDataIn.(int32)
		curIdx++
		if curIdx > 0 && curIdx < int32(len(self.children)) {
			return self.children[curIdx], RUNNING, curIdx
		} else {
			return nil, SUCCESS, nil
		}

	case FAILURE:
		return nil, FAILURE, nil
	}
	return
}
